redis的其他功能

慢查询

生命周期

image-20190802170403434

两点说明

  • 慢查询发生在第3阶段
  • 客户端超时不一定慢查询,但慢查询是客户端超时的一个可能因素

两个配置

  • slowlog-max-len

    image-20190802170532120

  • slowlog-log-slower-than

    image-20190802170625964

如何配置

image-20190802170725675

慢查询命令

  • slowlog get [n] : 获取慢查询队列
  • slowlog len: 获取慢查询队列长度
  • slowlog reset: 清空慢查询队列

运维经验

image-20190802170911283

pipeline:流水线

什么是流水线

  • 1次网络命令通信模型

image-20190802170953321

  • 批量网络命令通信模型

image-20190802171041404

image-20190802171136763

使用建议

  • 注意每次pipeline携带数据量
  • pipeline每次只能作用在一个redis节点上
  • M操作与pipeline区别

image-20190802171300428

image-20190802171350171

发布订阅

角色

  • 发布者
  • 订阅者
  • 频道

模型

image-20190802171655978

API

  • publish channel message
  • subscribe [channel] 一个或者多个
  • unsubscribe [channel] 一个或者多个

练习

1
2
3
4
5
127.0.0.1:6382> publish weibomovie "hello world"
(integer) 1
127.0.0.1:6382> publish weibomovie "hello world2"
(integer) 1
复制代码

另外一个cli

1
2
3
4
5
6
7
8
9
10
11
12
13
127.0.0.1:6382> SUBSCRIBE weibomovie
Reading messages... (press Ctrl-C to quit)
1) "subscribe"
2) "weibomovie"
3) (integer) 1
1) "message"
2) "weibomovie"
3) "hello world"
1) "message"
2) "weibomovie"
3) "hello world2"

复制代码

发布订阅与消息队列

image-20190802172241335

Bitmap

位图

paste image

paste image

API

命令 说明 时间复杂度
setbit key offset value 给位图指定索引设置值 O(1)
getbit key offset 获取位图指定索引的值 O(1)
bitcount key start end 获取位图指定范围(start 到end,单位为字节,如果不指定就获取全部)位值为1的个数 O(1)
bitop op destkey key [key…] 做多个bitmap的and,or,not,xor操作并将结果保存在destkey中 O(1)
bitpos key targetBit [start][end 计算位图指定范围(start到end,单位为字节,如果不指定就是获取全部)第一个偏移量对应的值等于targetBit的位置 O(1)

练习

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
127.0.0.1:6382> set hello big
OK
127.0.0.1:6382> getbit hello
(error) ERR wrong number of arguments for 'getbit' command
127.0.0.1:6382> getbit hello 0
(integer) 0
127.0.0.1:6382> setbit hello 0 1
(integer) 0
127.0.0.1:6382> get hello
"\xe2ig"
127.0.0.1:6382> set hell a
OK
127.0.0.1:6382> bitcount hell
(integer) 3
127.0.0.1:6382> bitop and hell hello
(integer) 3
127.0.0.1:6382> set a a
OK
127.0.0.1:6382> set b b
OK
127.0.0.1:6382> bitop and c a b
(integer) 1
127.0.0.1:6382> get c
"`"
127.0.0.1:6382> bitpos a 1
(integer) 1
127.0.0.1:6382> bitpos a 0
(integer) 0
127.0.0.1:6382> set user2 100
OK

复制代码

独立用户统计

重要理解使用位图去记录用户uid,其实就是记录索引值,比如userid=100代表位图下标100的值为1

image-20190802214149697

image-20190802214136935

使用经验

  • type=string,最大512MB
  • 注意setbit时的偏移量,可能有较大耗时
  • 位图不是绝对好.

https://blog.csdn.net/u011489043/article/details/78990162

https://my.oschina.net/mengyuankan/blog/1932425

https://segmentfault.com/a/1190000008188655

https://segmentfault.com/a/1190000008205145


HyperLogLog

新的数据结构

image-20190803141517629

API

命令 说明
pfaddd key element [element…] 向hyperloglog添加元素
pfcount key [key…] 计算hyperloglog的独立总数
pfmerge destkey sourceKey [sourcekey…] 合并多个hyperloglog

练习

1
2
3
4
5
6
7
8
9
10
11
12
127.0.0.1:6382> pfadd puser1 "u1" "u2" "u3"
(integer) 1
127.0.0.1:6382> pfcount puser1
(integer) 3
127.0.0.1:6382> pfadd puser2 "u3" "u4" "u5"
(integer) 1
127.0.0.1:6382> pfmerge puser puser1 puser2
OK
127.0.0.1:6382> pfcount puser
(integer) 5

复制代码

内存消耗

image-20190803141506383

使用经验

  • 是否能容忍错误(错误率:0.81%)
  • 是否需要单条数据(没有办法取出)

GEO

GEO是什么

![image-20190803141448122](../../../../../Users/apple/Library/Application Support/typora-user-images/image-20190803141448122.png)

5个城市经纬度

image-20190803141435412

API

命令 说明
geoadd key longitude latitude member [longitude latitude member …] 增加地理位置信息
geopos key member[member… 获取地理位置信息
geodist key member1 member2[unit] 获取两个地理位置的距离,unit:m,km,mi,ft
georadius 获取指定位置范围内的地理位置信息集合

练习

1
2
3
4
5
6
7
8
9
10
11
127.0.0.1:6382> geoadd beijing 116.28 39.55
(error) ERR wrong number of arguments for 'geoadd' command
127.0.0.1:6382> geoadd geo 116.28 39.55 beijing 117.12 39.08 tianjin
(integer) 2
127.0.0.1:6382> geopos geo beijing
1) 1) "116.28000229597091675"
2) "39.5500007245470826"
127.0.0.1:6382> geodist geo beijing tianjin
"89206.0576"
127.0.0.1:6382>
复制代码

相关说明

  • since 3.2+
  • type geoKey = zset
  • 没有删除API:zrem key member